<?php
/**
 * 后台首页
 */

namespace app\admin\controller;

use app\common\service\admin\MenuService;
use app\common\model\Admin as AdminModel;
use app\common\model\AuthGroup as AuthGroupModel;
use think\facade\Cache;
use think\facade\View;
use think\facade\Db;

class Index extends Base
{

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        //获取后台已登录的管理员id
        $adminId = cmf_get_admin_id();
        View::assign('admin_id', $adminId);

        return View::fetch();
    }

    /**
     * 后台菜单json 该方法用于public/static/admin/lib/pear/pear.config.yml
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function menuJson()
    {
        //获取缓存数据
        $cacheData = Cache::get('initAdmin_'.session('admin.id'));
        if ( ! empty($cacheData)) {
            $this->success('获取成功', $cacheData);
        }
        $menuService = new MenuService(cmf_get_admin_id());

        //获取后台菜单树信息
        $data = $menuService->getMenuTree();
        //给缓存initAdmin打个标记 todo 开启缓存
        Cache::tag('initAdmin')->set('initAdmin_'.session('admin.id'), $data);
        $this->success("获取成功", $data);
    }

    /**
     * 后台欢迎页 该方法用于public/static/admin/lib/pear/pear.config.yml
     * @return \think\response\View
     */
    public function welcome()
    {
        $adminId                 = cmf_get_admin_id();
        $roleIdArr               = AuthGroupModel::getAdminGroupNamesById($adminId);
        $adminInfo               = AdminModel::find($adminId);
        $adminInfo['group_name'] = implode(',', $roleIdArr);

        $this->assign('admin_info', $adminInfo);
        $this->assign('sys_info', $this->get_sys_info());

        return View::fetch("welcome");
    }

    /**
     * 后台清理缓存 该方法用于app/admin/view/index/index.html
     * @return void
     */
    public function clearCache()
    {
        Cache::clear();
        $this->success('清理缓存成功', "", 0, "");
    }

    /**
     * phpinfo信息 按需显示在前台
     * @return array
     */
    private function get_sys_info()
    {
        $sys_info['os']              = PHP_OS; //操作系统
        $sys_info['web_server']      = ''; //服务器环境
        $sys_info['phpv']            = phpversion(); //php版本
        $sys_info['fileupload']      = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknown'; //文件上传限制
        $sys_info['memory_limit']    = ini_get('memory_limit'); //最大占用内存
        $sys_info['domain']          = request()->host(); //域名
        $sys_info['remaining_space'] = round((@disk_free_space(".") / (1024 * 1024)), 2).'M'; //剩余空间
        $sys_info['beijing_time']    = gmdate("Y年n月j日 H:i:s", time() + 8 * 3600); //北京时间
        $sys_info['time']            = date("Y年n月j日 H:i:s"); //服务器时间
        $mysqlinfo                   = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version']   = $mysqlinfo[0]['version'];
        if (function_exists("gd_info")) {
            //GD库版本
            $gd                 = gd_info();
            $sys_info['gdinfo'] = $gd['GD Version'];
        } else {
            $sys_info['gdinfo'] = "未知";
        }

        return $sys_info;
    }

}
